package com.sptus.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sptus.common.annotation.DataSource;
import com.sptus.common.constant.HttpStatus;
import com.sptus.common.core.domain.AjaxResult;
import com.sptus.common.enums.DataSourceType;
import com.sptus.common.exception.ServiceException;
import com.sptus.framework.service.BaseCommonService;
import com.sptus.web.entity.SptUser;
import com.sptus.web.entity.SptUserAddress;
import com.sptus.web.mapper.slave.SptUserAddressMapper;
import com.sptus.web.mapper.slave.SptUserMapper;
import com.sptus.web.service.ISptUserAddressService;
import com.sptus.web.service.ISptUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: spt_user
 * @Author: jeecg-boot
 * @Date:   2024-09-24
 * @Version: V1.0
 */
@DataSource(DataSourceType.SLAVE)
@Service
//@DS("multi-datasource1")
public class SptUserServiceImpl extends ServiceImpl<SptUserMapper, SptUser> implements ISptUserService {

    @Autowired
    ISptUserAddressService sptUserAddressService;


    @Resource
    private BaseCommonService baseCommonService;
    @Override
    public AjaxResult checkUserIsEffective(SptUser one) {
        return AjaxResult.success();
    }

    @Override
    public List<SptUser> selectList(LambdaQueryWrapper<SptUser> sptUserLambdaQueryWrapper) {
        return this.getBaseMapper().selectList(sptUserLambdaQueryWrapper);
    }

    @Override
    public IPage<HashMap<String, Object>> getUserListPage(Page<SptUser> page, QueryWrapper<SptUser> lambdaQueryWrapper) {

        return this.getBaseMapper().getUserListPage(page,lambdaQueryWrapper);
    }

    @Override
    public void removeUserById(String id) {
         this.getBaseMapper().removeUserById(id);
    }

    @Override
    public ArrayList<HashMap<String, Object>> getUserClass() {
        return this.getBaseMapper().getUserClass();
    }

    @Override
    @Transactional
    public SptUser saveSptUser(SptUser sptUser) {
        LambdaQueryWrapper<SptUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SptUser::getEmail, sptUser.getEmail());
        queryWrapper.isNull(SptUser::getDeletedAt);
        List<SptUser> sptUsers = getBaseMapper().selectList(queryWrapper);
        if (sptUsers.size() > 0) {
            throw  new RuntimeException("添加异常");
        }
        boolean save = getBaseMapper().insert(sptUser) > 0;
        if (!save){
           throw  new RuntimeException("添加异常");
        }

        SptUser one = this.getOne(queryWrapper);
        SptUserAddress sptUserAddress = new SptUserAddress();
        sptUserAddress.setUserId(one.getId()+"");
        sptUserAddress.setCity(sptUser.getCity());
        sptUserAddress.setCountry(sptUser.getCountry());
        sptUserAddress.setPhone(sptUser.getPhone());
        sptUserAddress.setAddress(sptUser.getAddress());
        sptUserAddress.setFirstName(sptUser.getFirstName());
        sptUserAddress.setLastName(sptUser.getLastName());
        sptUserAddress.setZipcode(sptUser.getZipCode());
        sptUserAddress.setState(sptUser.getState());
        sptUserAddressService.save(sptUserAddress);
        return   one;
    }
}
